var Order;
if (!Order) {
    Order = {}
}
Order.pay = function (value, row, index) {
    var pay = row.payType;
    if (pay == 1) {
        return [
            '<div><span >' + '支付宝' + '</span></div> '
        ].join('');
    } else if (pay == 2) {
        return [
            '<div ><span >' + '微信' + '</span></div> '
        ].join('');
    } else if (pay == 3) {
        return [
            '<div ><span >' + '支付宝 + 红包' + '</span></div> '
        ].join('');
    } else if (pay == 4) {
        return [
            '<div ><span >' + '微信 + 红包' + '</span></div> '
        ].join('');
    } else {
        return [
            '<div ><span >' + '红包抵扣' + '</span></div> '
        ].join('');
    }
};


Order.status = function (value, row, index) {
    var state = row.orderState;
    if (state == 1) {
        state = '待付款';
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 2) {
        state = '待接单';
        return [
            '<div style="width: 100%"><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" style="color: red" onclick="agreeGoods(' + row.id + ',' + row.stock + ',' + row.goodsId + ',' + row.payType + ',' + row.count + ',\'' + row.orderNumber + '\',' + row.orderState + ')">' + "取消订单" + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);"  style="color: red" onclick="refuseOrder(' + row.id + ',' + row.buyerId + ', \'' + row.orderNumber + '\',' + row.payType + ')">' + "商家拒单" + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 3) {
        state = '待配送';
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" style="color: red" onclick="agreeGoods(' + row.id + ',' + row.stock + ',' + row.goodsId + ',' + row.payType + ',' + row.count + ',\'' + row.orderNumber + '\',' + row.orderState + ')">' + "取消订单" + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 4) {
        state = '配送中';
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo">' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 5) {
        state = '待评价';
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 6) {
        state = '已评价';
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 7) {
        state = '申请退款'; //取消订单
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" onclick="agreeGoods(' + row.id + ',' + row.stock + ',' + row.goodsId + ',' + row.payType + ',' + row.count + ',\'' + row.orderNumber + '\',' + row.orderState + ')">' + "同意退款" + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" style="color: red" onclick="refuse(' + row.id + ')">' + "拒绝退款" + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 8) {
        state = '商家拒单';
        var s = "";
        if (row.singleReason == null) {
            s = "";
        } else {
            s = row.singleReason;
        }
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);"  onclick="reason(' + row.id + ',\'' + s + '\')">' + "拒绝原因" + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 9) {
        state = '同意退款';
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 10) {
        state = '拒绝退款';
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo">' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 11) {
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 12) {
        state = '待取货';
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 13) {
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 14) {
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 15) {
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    } else if (state == 16) {
        return [
            '<div><a href="javascript:void(0);" class="orderDetails" >' + '订单详情' + '</a>' +
            '&nbsp;&nbsp;' +
            '<a href="javascript:void(0);" class="OrderInfo" >' + '订单信息' + '</a>' +
            // '&nbsp;&nbsp;' +
            // '<a href="javascript:void(0);" class="orderComment" >' + '订单评论' + '</a>' +
            '</div> '
        ].join('');
    }
};

// $(function () {
//     $('.clickTip').click(function () {
//         $(this).addClass('current').parent().siblings().find('a').removeClass('current');
//         var type = $(this).attr('name');
//
//         if (type == '' || type == undefined) {
//             var url = $().AbsUrlParse("/web/allOrder/queryOrderTable");
//         } else {
//             var url = $().AbsUrlParse("/web/allOrder/queryOrderTable?orderState=" + type);
//         }
//         var table = $("#table");
//         table.bootstrapTable("refresh", {url: url});
//     })
// })
//筛选
var queryNameOrder = function () {
    var name = $("#nameBusiness").val();
    var orderId = $("#orderId").val();
    var select = $("#select").val();
    var nameUser = $("#nameUser").val();
    var url = $().AbsUrlParse("/web/order/queryOrder?name=" + name + "&orderId=" + orderId + "&state=" + select + "&nameUser=" + nameUser);
    var table = $("#table");
    table.bootstrapTable("refresh", {url: url});
}

window.operateEvents = {
    'click .orderDetails': function (e, value, row, index) {
        showModel("findOrderDetail-div", function () {
            var url = $().AbsUrlParse("/web/order/queryOrderDetail?orderNumber=" + row.orderNumber);
            var table = $("#orderTable");
            table.bootstrapTable("refresh", {url: url});
            showPic = function (value, row, index) {
                var img = row.headPic;
                var imgUrl = "";
                if (img != null && img != '') {
                    imgUrl = File_Download_Url + img;
                    return [
                        '<image src=' + imgUrl + ' style="width:80px;height:80px"  onclick="enlargeImage(this)"/>'
                    ].join('');
                } else {
                    return [
                        '<image src=' + $().AbsUrlParse("/static/images/no.png") + ' style="width:80px;height:80px"  onclick="enlargeImage(this)"/>'
                    ].join('');
                }
            };
            goodsSpec = function (value, row, index) {
                var speac = row.goods_spec;
                var str = JSON.parse(speac);
                var goodsSpec = "";
                if (speac.length > 2) {
                    for (var i = 0; i < str.length; i++) {
                        //result[i]表示获得第i个json对象即JSONObject
                        //result[i]通过.字段名称即可获得指定字段的值
                        if (str[i].price > 0) {
                            goodsSpec = goodsSpec + '<div>' + '价格：' + str[i].price + ',&nbsp;&nbsp;' + '规格名：' + str[i].specName + ',&nbsp;&nbsp;' + '规格值：' + str[i].specValues + '</div>';
                            // return [
                            //     '<div>' + '价格：' + str[i].price + ',&nbsp;&nbsp;' + '规格名：' + str[i].specName + ',&nbsp;&nbsp;' + '规格值：' + str[i].specValues + '</div>'
                            // ].join('');
                        } else if (str[i].price == 0 || str[i].price == undefined) {

                            goodsSpec = goodsSpec +
                                '<div>' + '属性名：' + str[i].specName + ',&nbsp;&nbsp;' + '属性值：' + str[i].specValues + '</div>';
                            // return [
                            //     '<div>' + '属性名：' + str[i].specName + ',&nbsp;&nbsp;' + '属性值：' + str[i].specValues + '</div>'
                            // ].join('');
                        }
                    }
                    return [
                        goodsSpec
                    ].join('');

                }
            }
        });
    },
    'click .OrderInfo': function (e, value, row, index) {
        showModel("OrderInfo-div", function () {
            $("#id").val(row.id);
            $("#name").val(row.nickName);
            $("#account").val(row.account);
            $("#baccount").val(row.baccount);
            var account = row.daccount;
            if (account == null) {
                $("#daccount").val('暂无配送员电话');
            } else {
                $("#daccount").val(row.daccount);
            }
            $("#sendTime").val(row.sendTime);
            $("#endTime").val(row.endTime);
            $("#distance").val(row.distance);
            console.log(row.plannedDeliveryTime);
            if (row.plannedDeliveryTime == "" || row.plannedDeliveryTime == null) {
                console.log(row.plannedDeliveryTime);
                var time = '立即配送';
                $("#planTime").val(time);
            } else {
                $("#planTime").val(row.plannedDeliveryTime);
            }
            $("#fee").val(row.fee);
            $("#dispatching").val(row.dispatching);
            $("#goodsAdd").val(row.goodsAdress);
            if (row.refunds == 1) {
                var re = '支持';
            } else {
                var re = '不支持';
            }
            $("#refunds").val(re);
            $("#remark").val(row.remark);
        });
    },
    'click .orderComment': function (e, value, row, index) {
        showModel("orderComment-div", function () {
            var url = $().AbsUrlParse("/web/order/queryOrderComment?orderId=" + row.id);
            var table = $("#orderComment");
            table.bootstrapTable("refresh", {url: url});
            showImag = function (value, row, index) {
                var img = row.images;
                var imgUrl = "";
                if (img != null && img != '') {
                    imgUrl = File_Download_Url + img;
                    return [
                        '<image src=' + imgUrl + ' style="width:80px;height:80px"  onclick="enlargeImage(this)"/>'
                    ].join('');
                } else {
                    return [
                        '<image src=' + $().AbsUrlParse("/static/images/no.png") + ' style="width:80px;height:80px"  onclick="enlargeImage(this)"/>'
                    ].join('');
                }
            };
        });
    }
};

function agreeGoods(id, stock, goodsId, payType, count, orderNumber, orderState) {
    // if (orderState == 2) {
    //     var state = 11;
    // } else if (orderState == 3) {
    //     var state = 11;
    // } else if (orderState == 7) {
    //     var state = 9;
    // }
    $.post($().AbsUrlParse("/web/order/updateOrderState"), {
        id: id,
        state: orderState,
        stock: stock + count,
        goodsId: goodsId,
        payType: payType,
        orderNumber: orderNumber
    }, function () {
        if (orderState == 2) {
            hint('取消订单成功');
        } else if (orderState == 3) {
            hint('取消订单成功');
        } else if (orderState == 7) {
            hint('同意退款处理成功');
        }
        var table = $("#table");
        table.bootstrapTable("refresh");
    })
}

var refuse = function (id) {
    $.post($().AbsUrlParse("/web/order/updateOrderState"), {
        id: id,
        state: 10
    }, function () {
        hint('拒绝退款处理成功');
        //关闭model方法
        var table = $("#table");
        table.bootstrapTable("refresh");
    })
};

var refuseOrder = function (id, buyerId, orderNumber, payType) {
    showModel("fillReason-div", function () {
        $("#reasons").val('');
        $("#orderIds").val(id);
        $("#state").val(8);
        $("#buyerId").val(buyerId);
        $("#orderNumber").val(orderNumber);
        $("#payType").val(payType);
    });
};

var reason = function (id, reason) {
    showModel("orderReason-div", function () {
        $("#ids").val(id);
        $("#reason").val(reason);
    })
};

fee = function (value, row, index) {
    if (row.fee == null) {
        return [
            '<div><span >' + 0.00 + '</span></div> '
        ].join('');
    } else {
        return [
            '<div ><span >' + row.fee + '</span></div> '
        ].join('');
    }
};

orderState = function (value, row, index) {
    if (row.orderState == 1) {
        var s = "待付款";
    } else if (row.orderState == 2) {
        var s = "待接单";
    } else if (row.orderState == 3) {
        var s = "待配送";
    } else if (row.orderState == 4) {
        var s = "配送中";
    } else if (row.orderState == 5) {
        var s = "待评价";
    } else if (row.orderState == 6) {
        var s = "已完结";
    } else if (row.orderState == 7) {
        var s = "申请退款";
    } else if (row.orderState == 8) {
        var s = "拒绝接单";
    } else if (row.orderState == 9) {
        var s = "同意退款";
    } else if (row.orderState == 10) {
        var s = "拒绝退款";
    } else if (row.orderState == 11) {
        var s = "用户取消";
    } else if (row.orderState == 12) {
        var s = "待取货";
    } else if (row.orderState == 13) {
        var s = "申请退货";
    } else if (row.orderState == 14) {
        var s = "同意退货";
    } else if (row.orderState == 15) {
        var s = "拒绝退货";
    } else if (row.orderState == 16) {
        var s = "货物已归还商家";
    }
    return [
        '<div><span >' + s + '</span></div> '
    ].join('');

};

deliveryName = function (value, row, index) {
    var name = row.deliveryName;
    if (name == null) {
        if (row.dispatching == 0) {
            return [
                '<div><span style="color: orangered">' + '商家自配送 ' + '</span></div> '
            ].join('');
        } else {
            return [
                '<div><span style="color: orangered">' + '暂无配送员名称 ' + '</span></div> '
            ].join('');
        }

    } else {
        return [
            '<div><span >' + row.deliveryName + '</span></div> '
        ].join('');
    }
};

daccount = function (value, row, index) {
    var account = row.daccount;
    if (account == null) {
        return [
            '<div><span style="color: orangered">' + '暂无配送员电话 ' + '</span></div> '
        ].join('');
    } else {
        return [
            '<div><span >' + row.daccount + '</span></div> '
        ].join('');
    }
};

/**
 * 添加按钮事件
 */
bindClick("button[data-event=submit-updateReason]", function () {
    var reasons = $("#reasons").val();
    if (reasons != null && reasons != "") {
        var form = $("#reasonFrom");
        var param = form.formToArray();
        $.post($().AbsUrlParse('/web/order/refuseOrder'), param, function (res) {
            if (res.state && res.res.code == 40000) {
                hint('商家拒单成功');
                //关闭model方法
                closeModel('fillReason-div');
                var table = $("#table");
                table.bootstrapTable("refresh");
            } else {
                if (res.state == false) {
                    hint(res.msg);
                } else {
                    hint(res.res.msg);
                }
            }
        });
    } else {
        hint("请填写拒单原因");
    }

});

function enlargeImage(value) {
    showModel("detailImage-div", function () {
        var imgs = '<image src=' + value.src + ' style="height:500px" />';
        $("#image").html(imgs);
    })
}
